package com.css.fxfzxczh.modules.fieldWorkManage.lnvestigationTaskMange.entity.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzxczh.base.response.RestResponse;

import com.css.fxfzxczh.modules.fieldWorkManage.lnvestigationTaskMange.entity.InvestigationTask;
import com.css.fxfzxczh.modules.fieldWorkManage.lnvestigationTaskMange.service.InvTaskManageService;
import com.css.fxfzxczh.modules.fieldWorkManage.lnvestigationTaskMange.service.impl.InvTaskManageServiceImpl;
import com.css.fxfzxczh.modules.fieldWorkManage.lnvestigationTaskMange.vo.InvestigationTaskVo;
import com.css.fxfzxczh.modules.fieldWorkManage.lnvestigationTaskMange.vo.UpdateVo;
import com.css.fxfzxczh.util.PlatformPageUtils;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @program
 * @description
 * @create 2022/8/8 10:39
 **/
@RestController
@RequestMapping("/invTaskManage")
@Slf4j
public class InvTaskManageController {
    @Resource
    InvTaskManageService invTaskManageService;

    /**
     * 分页列表
     *
     * @param
     * @return
     */
    @GetMapping("/getPage/{xsemId}")
    public RestResponse getPage(HttpServletRequest request,@PathVariable("xsemId") String xsemId) {
        RestResponse restResponse = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = invTaskManageService.getPage(curPage, pageSize,xsemId);
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 任务列表
     *
     * @param
     * @return
     */
    @GetMapping("/getTaskList")
    public RestResponse getTaskList(String xsemId,String taskType) {
        RestResponse restResponse = null;
        try {
            List<InvestigationTaskVo> page = invTaskManageService.getTaskList(xsemId,taskType);
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 保存
     *
     * @param investigationTask
     * @return
     */
    @PostMapping("/save")
    public RestResponse save(@RequestBody InvestigationTask investigationTask) {
        RestResponse response = null;
        try {
            invTaskManageService.save(investigationTask);
            response = RestResponse.succeed();
            response.setMessage("保存成功");
        } catch (Exception e) {
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "保存失败";
            }
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 修改
     *
     * @param investigationTask
     * @return
     */
    @PostMapping("/update")
    public RestResponse update(@RequestBody InvestigationTask investigationTask) {
        RestResponse response = null;
        JSONObject jsonObject = new JSONObject();
        try {
            invTaskManageService.update(investigationTask);
            response = RestResponse.succeed();
            response.setMessage("修改成功");
        } catch (Exception e) {
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "修改失败";
            }
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 查看
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public RestResponse findById(@RequestParam("id") String id) {
        RestResponse response = null;
        try {
            InvestigationTask investigationTask=invTaskManageService.findById(id);
            response = RestResponse.succeed(investigationTask);
        } catch (Exception e) {
            String errorMessage = "查看失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public RestResponse deleteById(@PathVariable("id") List<String> id) {
        RestResponse response = null;
        JSONObject jsonObject = new JSONObject();
        try {
            invTaskManageService.deleteById(id);
            response = RestResponse.succeed();
            response.setMessage("删除成功");
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 根据地震事件id删除扇形任务
     *
     * @param xsemId
     * @return
     */
    @PostMapping("/deleteByXsemId/{xsemId}")
    public RestResponse deleteByXsemId(@PathVariable("xsemId") String xsemId) {
        RestResponse response = null;
        try {
            invTaskManageService.deleteByXsemId(xsemId);
            response = RestResponse.succeed();
            response.setMessage("删除成功");
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 调查任务编号
     * @return
     */
    @GetMapping("/getCode")
    public RestResponse getCode(){
        RestResponse response = null;

        try{
            Map<String,Object> map=invTaskManageService.getCode();
            response = RestResponse.succeed(map);
        }catch (Exception e){
            String errorMessage = "生成失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 根据地图圈选区域，查询行政区划--省|市|区县的名称和code列表
     */
    @PostMapping("/getAdministrativeDivisionByGeomText")
    public RestResponse getAdministrativeDivisionByGeomText(@RequestBody Map<String,String> map){
        RestResponse restResponse = null;
        try{
            restResponse = RestResponse.succeed(invTaskManageService.getAdministrativeDivisionByGeomText(map.get("geomText")));
        }catch (Exception e){
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }

    /**
     * 根据小组分配任务
     *
     * @param list
     * @return
     */
    @PostMapping("/accTotheGroupSave")
    public RestResponse accTotheGroupSave(@RequestBody List<InvestigationTask> list) {
        RestResponse response = null;

        try {
            invTaskManageService.accTotheGroupSave(list);
            response = RestResponse.succeed();
            response.setMessage("保存成功");
        } catch (Exception e) {
            String errormessage = "保存失败";
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 获取app任务分页列表
     *
     * @param
     * @return
     */
    @GetMapping("/getAppTaskList/{xsemId}/{userId}")
    public RestResponse getAppTaskList(HttpServletRequest request,@PathVariable("xsemId") String xsemId,@PathVariable("userId") String userId) {
        RestResponse restResponse = null;
        try {
            List<InvestigationTaskVo> list = invTaskManageService.getAppTaskList(xsemId,userId);
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 获取app任务分页列表
     *
     * @param
     * @return
     */
    @GetMapping("/getAppPageTaskList/{xsemId}/{userId}")
    public RestResponse getAppPageTaskList(HttpServletRequest request,@PathVariable("xsemId") String xsemId,@PathVariable("userId") String userId) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = invTaskManageService.getAppPageTaskList(xsemId,userId,curPage,pageSize);
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     * 任务状态修改
     * @param
     * @return
     */
    @PostMapping("/updateTaskStatu")
    public RestResponse updateTaskStatu(@RequestBody UpdateVo updateVo){
        RestResponse response = null;
        //JSONObject jsonObject=new JSONObject();
        try{
            invTaskManageService.updateTaskStatu(updateVo);
            //jsonObject.put("message","修改成功");
            response = RestResponse.succeed();
        }catch (Exception e){
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "修改失败";
            }
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }


}
